﻿using ProsonUser.IProsonUser;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ProsonUser.Users;
using System.Threading.Tasks;
using ProsonUser.ProsonUserDTO;
using Volo.Abp.ObjectMapping;
using ecd_csharp_demo;

namespace ProsonUser.ProsonUser
{
    public  class PUser: ApplicationService, IUser
    {
        private readonly IRepository<User, int> _users;
        public PUser(IRepository<User, int> users) 
        {
            _users = users;
        }


        [HttpGet]
        public async Task<int> ListUser(string userName,string userPwd)
        {
            var lst =  _users.Where(s => s.UserPhone.Equals(userName) && s.UserPwd.Equals(userPwd)).Count();
            //return ObjectMapper.Map<List<User>, List<UserDTO>>(lst);
            if (lst>0)
            {
                return 1;
            }
            var lst1 = _users.Where(s => s.UserEmail.Equals(userName) && s.UserPwd.Equals(userPwd)).Count();
            if(lst1>0)
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }
        [HttpPut]
        public async Task<int> UpdatePossword(UserDTO dTO)
        {
            User user1 = _users.Where(s => s.Id.Equals(dTO.Id)).FirstOrDefault();
            User user = await _users.GetAsync(dTO.Id);
            user.UserPwd = dTO.UserPwd;
            return 1;
        }
        [HttpGet]
        public UserDTO UserFind(int Id)
        {
            var list = _users.Where(S => S.Id.Equals(Id)).FirstOrDefault();
            return ObjectMapper.Map<User, UserDTO>(list);
        }
        [HttpPut]
        public async Task<int> UpdateImg(UserCreateDTO dTO)
        {
            User user1 = _users.Where(s => s.Id.Equals(dTO.Id)).FirstOrDefault();
            User user = await _users.GetAsync(user1.Id);
            user.UserImg = dTO.UserImg;
            return 1;
        }
        [HttpPut]
        public async Task<int> UpdateUser(UserDTO dTO)
        {
            User user1 = _users.Where(s => s.Id.Equals(dTO.Id)).FirstOrDefault();
            User user = await _users.GetAsync(dTO.Id);
            user.UserName = dTO.UserName;
            user.UserPhone = dTO.UserPhone;
            user.UserEmail = dTO.UserEmail;
            return 1;
        }
        [HttpGet]
        public UserDTO UserShow(string UserPhone,string UserEmail)
        {
            var list = _users.Where(S => S.UserPhone.Equals(UserPhone)||S.UserEmail.Equals(UserEmail)).FirstOrDefault();
            return ObjectMapper.Map<User, UserDTO>(list);
        }
        [HttpPost]
        public async Task<int> Roll(UserCreateDTO userDTO)
        {
            //var info = await _repository.InsertAsync(ObjectMapper.Map<CreateUpdateImgDTO, tbCommodityInfoImg>(imgDTO));
            //return ObjectMapper.Map<tbCommodityInfoImg, ProductImgDTO>(info);
            var lst = await _users.InsertAsync(ObjectMapper.Map<UserCreateDTO, User>(userDTO));
            if(lst!=null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        [HttpPost]
        public int message(string userPhone)
        {
            var ret = ecd_csharp_demo.CloudInfDemo.sendSmsCode(userPhone, 1, "8742").Count();
            return ret;
        }
    }
}
